home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / Src / Ch9 / SpRect.cls < prev    next >
Encoding:
Visual Basic class definition  |  1999-05-28  |  2.9 KB  |  109 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4.   Persistable = 0  'NotPersistable
  5.   DataBindingBehavior = 0  'vbNone
  6.   DataSourceBehavior  = 0  'vbNone
  7.   MTSTransactionMode  = 0  'NotAnMTSObject
  8. END
  9. Attribute VB_Name = "RectangleSprite"
  10. Attribute VB_GlobalNameSpace = False
  11. Attribute VB_Creatable = False
  12. Attribute VB_PredeclaredId = False
  13. Attribute VB_Exposed = False
  14. ' Moving rectangle sprite.
  15. Option Explicit
  16.  
  17. Implements Sprite
  18.  
  19. Private Wid As Integer
  20. Private Hgt As Integer
  21. Private Cx As Integer       ' Position of center.
  22. Private Cy As Integer
  23. Private Vx As Integer       ' Velocity.
  24. Private Vy As Integer
  25. Private Theta As Single     ' Orientation.
  26. Private Vtheta As Single    ' Angular velocity.
  27. Private Color As Long
  28.  
  29. Private Type POINTAPI
  30.     x As Long
  31.     y As Long
  32. End Type
  33. Private Declare Function Polygon Lib "gdi32" (ByVal hdc As Long, lpPoint As POINTAPI, ByVal nCount As Long) As Long
  34. ' Draw the rectangle on the indicated picture box.
  35. Public Sub Sprite_DrawSprite(ByVal pic As PictureBox)
  36. Const PI = 3.14159265
  37. Const PI_OVER_2 = PI / 2
  38.  
  39. Dim wx As Single
  40. Dim wy As Single
  41. Dim hx As Single
  42. Dim hy As Single
  43. Dim pts(1 To 4) As POINTAPI
  44. Dim status As Long
  45. Dim newpen As Long
  46. Dim oldpen As Long
  47. Dim newbrush As Long
  48. Dim oldbrush As Long
  49.  
  50.     ' Compute vectors parallel to the axes.
  51.     wx = Wid * Cos(Theta)
  52.     wy = Wid * Sin(Theta)
  53.     hx = Hgt * Cos(Theta + PI_OVER_2)
  54.     hy = Hgt * Sin(Theta + PI_OVER_2)
  55.     
  56.     pts(1).x = Cx + (wx + hx) / 2
  57.     pts(1).y = Cy + (wy + hy) / 2
  58.     pts(2).x = pts(1).x - hx
  59.     pts(2).y = pts(1).y - hy
  60.     pts(3).x = pts(2).x - wx
  61.     pts(3).y = pts(2).y - wy
  62.     pts(4).x = pts(3).x + hx
  63.     pts(4).y = pts(3).y + hy
  64.  
  65.     ' Draw the rectangle.
  66.     pic.FillColor = Color
  67.     pic.ForeColor = Color
  68.     Polygon pic.hdc, pts(1), 4
  69. End Sub
  70.  
  71. ' Initialize the rectangle.
  72. Public Sub InitializeRectangle(ByVal new_wid As Integer, ByVal new_hgt As Integer, ByVal new_cx As Integer, ByVal new_cy As Integer, ByVal new_vx As Integer, ByVal new_vy As Integer, ByVal new_theta As Single, ByVal new_vtheta As Single, ByVal new_color As Long)
  73.     Wid = new_wid
  74.     Hgt = new_hgt
  75.     Cx = new_cx
  76.     Cy = new_cy
  77.     Vx = new_vx
  78.     Vy = new_vy
  79.     Theta = new_theta
  80.     Vtheta = new_vtheta
  81.     Color = new_color
  82. End Sub
  83. ' Add the velocity components to the sprite's
  84. ' position components.
  85. Public Sub Sprite_MoveSprite(ByVal xmin As Integer, ByVal xmax As Integer, ByVal ymin As Integer, ByVal ymax As Integer)
  86.     Cx = Cx + Vx
  87.     Cy = Cy + Vy
  88.  
  89.     ' Keep the object within the drawing area.
  90.     If (Cx < xmin) Then
  91.         Cx = 2 * xmin - Cx
  92.         Vx = -Vx
  93.     ElseIf (Cx > xmax) Then
  94.         Cx = 2 * xmax - Cx
  95.         Vx = -Vx
  96.     End If
  97.     If (Cy < ymin) Then
  98.         Cy = 2 * ymin - Cy
  99.         Vy = -Vy
  100.     ElseIf (Cy > ymax) Then
  101.         Cy = 2 * ymax - Cy
  102.         Vy = -Vy
  103.     End If
  104.  
  105.     Theta = Theta + Vtheta
  106. End Sub
  107.  
  108.  
  109.